package com.caifu.timer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.bean.Result;
import com.caifu.mapper.UserInfoMapper;
import com.caifu.mapper.YlDayBillMapper;
import com.caifu.mapper.YlElderlyInfoMapper;
import com.caifu.pojo.UserInfo;
import com.caifu.pojo.YlDayBill;
import com.caifu.pojo.YlElderlyInfo;
import com.caifu.service.IBillService;
import com.caifu.util.AesMyUtil;
import com.caifu.util.CheckIdCardUtil;
import com.caifu.util.Util;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 标题：跑日结算
 * 描述：查询每日有效的长护险，床位费，护理费，伙食费跑日账单
 * 创建：szt
 */
@Configuration
@EnableScheduling
public class BiilTimer {

    @Resource
    private YlElderlyInfoMapper elderMapper;
    @Autowired
    private IBillService billService;


    /**
     * 生成日结算账单
     * 2020/11/25
     * codeBy szt
     */
    @Scheduled(cron = "4 0 0 * * ?")
    //@PostConstruct
    private void createDayBill(){
        try {
            final String yesterday = Util.dateCalculation(Util.currentDate(),"yyyyMMdd",-1);
            //System.out.println("yesterday="+yesterday);
            //老人档案 计算年龄
            List<Map<String, Object>> elderList = elderMapper.selectMaps(new QueryWrapper<YlElderlyInfo>().eq("USER_STATE","1").orderByDesc("CREATE_DATE").orderByAsc("USER_NO"));
            elderList.forEach(elder->{
                String userNo = (String) elder.get("USER_NO");
                try {
                    //生成日账单
                    Result rst = billService.createDayBill(yesterday, userNo);
                    if(rst.getCode()!=200){
                        System.out.println("生成日账单失败, "+rst.getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            System.out.println("执行静态定时任务时间: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm:ss")) + "---账务管理-生成日结算账单:"+yesterday);
        } catch (Exception e) {
            System.out.println("定时器生成日结算账单 错误："+e.getMessage());
            e.printStackTrace();
        }

    }


}
